import AMapLoader from '@amap/amap-jsapi-loader';

type IAny = { [T: string]: any };
type Path = number[][];
type GridItem<T> = {
    id?: string;
    name?: string;
    parentId?: string;
    coorId?: string;
    parentName: string;
    data: T;
};
type GridFGItem = {
    parentName: string;
    position: [number, number];
    data: number[][];
};

// 加载地图
export const loadAMap = () => {
    return AMapLoader.load({
        key: 'ac2aa06389621e08b8c5fb0b63c95f53',
        version: '2.0',
        plugins: ['AMap.PolygonEditor']
    });
};

// 创建多边形
export const createPolygon = (AMap: IAny) => {
    return (path: Path, options?: object) => {
        const polygon = new AMap.Polygon({
            path,
            strokeOpacity: 1,
            fillOpacity: 0.5,
            strokeColor: '#2b8cbe',
            strokeWeight: 2,
            strokeStyle: 'dashed',
            ...options
        });
        // polygon.on('mouseover', () => {
        //     polygon.setOptions({
        //         fillOpacity: 0.7,
        //         fillColor: '#7bccc4'
        //     });
        // });
        // polygon.on('mouseout', () => {
        //     polygon.setOptions({
        //         fillOpacity: 0.5,
        //         fillColor: '#ccebc5'
        //     });
        // });
        return polygon;
    };
};

// 创建文本
export const createText = (AMap: IAny) => {
    return (position: [number, number], options?: object) => {
        const text = new AMap.Text({
            // anchor: 'bottom-left',
            draggable: true,
            cursor: 'pointer',
            style: {
                color: 'black',
                'font-weight': 'bolder',
                'border-width': 0,
                'background-color': 'rgba(255,255,255,0)'
            },
            position,
            ...options
        });
        return text;
    };
};

// 创建标点
export const createMarker = (AMap: IAny) => {
    return (position: [number, number], options?: object) => {
        const marker = new AMap.Marker({
            // position: new AMap.LngLat(position[0], position[1]),
            position,
            ...options
        });
        return marker;
    };
};

// 创建线条
export const createLine = (AMap: IAny) => {
    return (path: [number, number][], options?: object) => {
        const line = new AMap.Polyline({
            path,
            strokeColor: 'red',
            strokeWeight: 5,
            strokeStyle: 'dashed',
            ...options
        });
        return line;
    };
};